/******************************************************************************
 * 
 * Announce: CSharpKit, Basic algorithms, components and definitions.
 *           Copyright (C) ShenYongchen.
 *           All rights reserved.
 *   Author: 申永辰.郑州 (shenyczz@163.com)
 *  WebSite: http://gitee.com/shenyczz/csharp_csk
 *
 * THIS CODE IS LICENSED UNDER THE MIT LICENSE (MIT).
 * THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF 
 * ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
 * IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
 * PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
 * 
******************************************************************************/

using System;
using System.Runtime.InteropServices;

namespace CSharpKit
{
    [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Auto, Pack = 1, Size = 8)]
    public struct UnionBytesInt64 : IUnion
    {
        [FieldOffset(0)]
        byte b0;
        [FieldOffset(1)]
        byte b1;
        [FieldOffset(2)]
        byte b2;
        [FieldOffset(3)]
        byte b3;
        [FieldOffset(4)]
        byte b4;
        [FieldOffset(5)]
        byte b5;
        [FieldOffset(6)]
        byte b6;
        [FieldOffset(7)]
        byte b7;

        [FieldOffset(0)]
        Int64 Value;

        public byte[] GetBytes()
        {
            return new byte[] { b0, b1, b2, b3, b4, b5, b6, b7 };
        }

        public void SetBytes(byte[] bytes, int offset)
        {
            try
            {
                b0 = bytes[offset + 0];
                b1 = bytes[offset + 1];
                b2 = bytes[offset + 2];
                b3 = bytes[offset + 3];
                b4 = bytes[offset + 4];
                b5 = bytes[offset + 5];
                b6 = bytes[offset + 6];
                b7 = bytes[offset + 7];
            }
            catch (System.Exception)
            {
                // throw;
            }
        }

        public T GetValue<T>()
        {
            return (T)(dynamic)Value;
        }

        public void SetValue<T>(T value)
        {
            Value = (dynamic)value;
        }




        public static explicit operator Int64(UnionBytesInt64 union) => union.Value;
        public static explicit operator UnionBytesInt64(Int64 value) => new UnionBytesInt64() { Value = value };


        // {{END}}
    }


}
